package com.jinlejia.erp.ui.new_house.adapter;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * 房源详情户型图Adapter
 */

public class HorizontalHouseTypeAdapter extends YfListAdapter<Map<String,Object>> {

    private Context context;

    public HorizontalHouseTypeAdapter(Context context, ArrayList<Map<String, Object>> data) {
        super(data);
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_horizontal_type,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = (TextView) view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("picUrl")))) {
            Glide.with(context).load(ApiConfig.MAGEHEADER_URL().concat(mData.get(position).get("picUrl").toString()))
                    .placeholder(R.mipmap.ic_no_img).into(((ViewHolder) holder).house_image);
        }
        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("houseType")))) {
            ((ViewHolder) holder).title.setText(String.valueOf(mData.get(position).get("houseType")));
        }
        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("acreage")))) {
            if (mData.get(position).containsKey("saleState")) {
                if (Integer.parseInt(String.valueOf(mData.get(position).get("saleState"))) == 1) {
                    ((ViewHolder) holder).tvArea.setText("待售" + mData.get(position).get("acreage") + "㎡");
                    ((ViewHolder) holder).tvHouseTypeState.setText("待售");
                    ((ViewHolder) holder).tvHouseTypeState.setBackgroundColor(Color.parseColor("#FF8000"));
                } else {
                    ((ViewHolder) holder).tvArea.setText("在售" + mData.get(position).get("acreage") + "㎡");
                    ((ViewHolder) holder).tvHouseTypeState.setText("在售");
                    ((ViewHolder) holder).tvHouseTypeState.setBackgroundColor(Color.parseColor("#FAA700"));
                }
            }
        }
        if (!TextUtils.isEmpty(String.valueOf(mData.get(position).get("price")))&&!String.valueOf(mData.get(position).get("price")).equals("null")){
            if (String.valueOf(mData.get(position).get("price")).equals("0.0")||String.valueOf(mData.get(position).get("price")).equals("0")){
                ((ViewHolder) holder).tvPrice.setText("价格待定");
            }else{
                ((ViewHolder) holder).tvPrice.setText("约" + mData.get(position).get("price") + "万/套");
            }
        }else{
            ((ViewHolder) holder).tvPrice.setText("价格待定");
        }

        ((ViewHolder) holder).house_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                double id = Double.parseDouble(String.valueOf(mData.get(position).get("id")));
                int i = Integer.parseInt(new DecimalFormat("0").format(id));
                if (listener!=null){
                    listener.OnImageClick(i);
                }
            }
        });
        holder.itemView.setTag(mData.get(position));
    }

    private static final class ViewHolder extends RecyclerView.ViewHolder{

        ImageView house_image;
        TextView title, tvHouseTypeState, tvArea, tvPrice;

        public ViewHolder(View itemView) {
            super(itemView);
            house_image = itemView.findViewById(R.id.house_image);
            title = itemView.findViewById(R.id.tvTitle);
            tvHouseTypeState = (TextView) itemView.findViewById(R.id.tvHouseTypeState);
            tvArea = (TextView) itemView.findViewById(R.id.tvArea);
            tvPrice = (TextView) itemView.findViewById(R.id.tvPrice);
        }
    }

    public interface OnImageListener{
        void OnImageClick(int position);
    }

    private OnImageListener listener;

    public void OnImagesListener(OnImageListener listener){
        this.listener = listener;
    }
}
